package com.aixin.csopsupplier.service;

import com.aixin.common.core.domain.model.LoginBody;
import com.aixin.common.core.redis.RedisCache;
import com.aixin.csopsupplier.domain.SupplierDef;
import com.aixin.csopsupplier.mapper.SupplierConfigMapper;
import com.aixin.csopsupplier.model.SupplierDefModel;
import com.aixin.csopsupplier.model.SystemReturn;
import com.aixin.csopsupplier.repository.SupplierDefRepository;
import com.aixin.csopsupplier.utils.Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
public class SupplierDefService {

    @Autowired
    private SupplierDefRepository supplierDefRepository;
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private SupplierConfigMapper supplierConfigMapper;


    public List<SupplierDef> findAll() {
        return supplierDefRepository.findAll();
    }

    public SystemReturn save(SupplierDefModel supplierDefModel) {
        if (supplierDefModel != null) {
            SupplierDef supplierDef = new SupplierDef();
            supplierDef.setSupplierID(Util.getUUID());
            supplierDef.setSupplierName(supplierDefModel.getSupplierName());
            supplierDef.setRemarks(supplierDefModel.getRemarks());
            supplierDef.setStartTime(supplierDefModel.getStartTime());
            supplierDef.setEndTime(supplierDefModel.getEndTime());
            supplierDef.setCreateTime(Util.getNowDate());
            supplierDef.setStatus(supplierDefModel.getStatus());
            supplierDef.setUserName(supplierDefModel.getUserName());
            supplierDef.setPassword(DigestUtils.md5DigestAsHex(supplierDefModel.getPassword().getBytes()));
            supplierDef = supplierDefRepository.save(supplierDef);
            if (supplierDef != null) {
                return SystemReturn.getSystemTrueReturn();
            }
        }
        return SystemReturn.getSystemFalseReturn();
    }


    @Transactional
    public SystemReturn update(SupplierDefModel supplierDef) {
        int flog = supplierDefRepository.updateSupplierDef(
                supplierDef.getSupplierName(), supplierDef.getRemarks(),
                supplierDef.getStartTime(), supplierDef.getEndTime(), supplierDef.getStatus(),
                supplierDef.getSupplierID(), Util.getNowDate());
        if (flog > 0) {
            return SystemReturn.getSystemTrueReturn();
        }
        return SystemReturn.getSystemFalseReturn();
    }

    public PageInfo<SupplierDef> findAll(SupplierDefModel supplierDefModel, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List list = supplierConfigMapper.getSupplierDef(supplierDefModel);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    public SupplierDef loginSupplier(LoginBody loginBody) {
        SupplierDef supplierDef = supplierDefRepository.supplierLogin(loginBody.getUsername(), DigestUtils.md5DigestAsHex(loginBody.getPassword().getBytes()));
        return supplierDef;
    }

    public SystemReturn updatePassword(SupplierDefModel supplierDefModel) {
        supplierDefRepository.updatePassword(supplierDefModel.getSupplierID(), DigestUtils.md5DigestAsHex(supplierDefModel.getPassword().getBytes()));
        return SystemReturn.getSystemTrueReturn();
    }

    public boolean getSupplierByUserName(String userName) {
        int num = supplierConfigMapper.getSupplierByUserName(userName);
        if (num > 0) {
            return false;
        }
        return true;
    }


}
